// pages/mine/mine.js
const App = getApp();
const navigationBarHeight = (App.statusBarHeight + 44) + 'px'
var config = require('../../utils/config');
var net = require('../../utils/net.js');
var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navigationBarTitle: 'マイページ',
    navigationBarHeight,


    navbar: ['保存した特典', '使用済み特典'],
    currentTab: 0,
    showLeftModal: false,
    showRightModal: false,
    userInfo:{},
    userPhoto:'',
    dataArr:[],
    selectItem:[],
    scrollTop: 0,
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    new App.ToastPannel();
    this.page = 1;
    this.totalPage = 1;
    this.state = 0;//0保存，1使用
    
    this.requestMyList();
  
  },

  onShow:function(){
    this.requestUserInfo();
  },
  requestUserInfo:function(){
    var url = config.service.userInfoUrl;
    var params = {

    }
    net.doRequest(url, params, 'GET', true, net.Token, res => {
      
      if(res.code == 0){
        this.setData({
          userInfo: res.data,
          userPhoto:App.globalData.userPhoto
        })
      }else{
        this.show(res.mesage);
      }  
    })
  },

  requestMyList:function(){
    var url = config.service.myDiscountUrl;
    var params = {
      page:this.page,
      size:10,
      state:this.state,
    }
    net.doRequest(url, params, 'GET', true, net.Token, res => {
      wx.stopPullDownRefresh();
      if (res.code == 0) {
        this.totalPage = res.data.totalPage
        var shopArr = res.data.items;
        if(shopArr.length > 0){
          for(var i = 0; i<shopArr.length; i++){
            var list = shopArr[i].wCustomerDiscountDtoOutList;
            for(var j = 0; j < list.length; j++){
              var item = list[j]
              if (!util.isNull(item.limitedAt)) {
                item.limitedAtStr = util.zhuanHuaTime(item.limitedAt, 'Y年M月D日')
              }
            }
          } 
        }
        console.log('数组',shopArr);
        if(this.page == 1){
          this.setData({
            dataArr:shopArr
          })
        }else{
          this.setData({
            dataArr: this.data.dataArr.concat(shopArr)
          })
        }   
      } else {
        this.show(res.mesage);
      }
    })
  },

 /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.page = 1;
    this.requestMyList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.page == this.totalPage){
      this.show('没有更多');
      return;
    }
    this.page ++;
    this.requestMyList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  editClick: function () {
    wx.navigateTo({
      url: './edit',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    });
  },

  /**
   * navBar点击
   */
  navbarTap: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
    this.page = 1;
    this.totalPage = 1;
    this.state = e.currentTarget.dataset.idx;
    this.setData({
      dataArr:[]
    })
    this.requestMyList();
  },

  sectionHeadClick: function (e) {
    var index = e.currentTarget.dataset.index;
    var shopId = this.data.dataArr[index].merchantId;
    var shopName = this.data.dataArr[index].shopName;
    wx.navigateTo({
      url: '../index/indexDetail?shopId='+shopId+'&shopName='+shopName,
    })
  },

  leftItemClick: function (e) {
    var item = e.currentTarget.dataset.item;
    if (this.data.currentTab == 0){
      this.setData({
        selectItem: item,
        showLeftModal: true,
      })
    }else{
      this.setData({
        selectItem: item,
        showRightModal: true,
      })
    }
    
  },

  zhongwenClick: function () {
    this.setData({
      showRightModal: false,
      showLeftModal: false,
    })
    wx.navigateTo({
      url: './fanyi?model='+JSON.stringify(this.data.selectItem),
    })

  },
  shanchuClick: function () {
    this.setData({
      showLeftModal: false,
    })
    var customerDiscountId = this.data.selectItem.customerDiscountId
    var url = config.service.host + '/wechat/discount/'+customerDiscountId+'/delete.do'
    var params = {
      customerDiscountId: customerDiscountId
    }

    net.doRequest(url, params, 'DELETE', true, net.Token, res => {
      if (res.code == 0) {
        this.show('删除成功');
        this.requestMyList();
      } else {
        this.show(res.mesage);
      }
    })

  },

  modeTap: function () {
    this.setData({
      showLeftModal: false,
      showRightModal: false,

    })
  },
  getPhoneNumber:function(e){
    var that = this;
    wx.login({
      success: res => {
        if (res.code) {
          this.getSesssionKey(res.code, e.detail.encryptedData, e.detail.iv,e);
        }
      },
      fail: function () {
        
      }
    });
  
  },

  getSesssionKey(code,encryData,ivData,e){
    var pa = {
      code:code
    }

    net.doRequest(config.service.getSessionKey, pa, 'POST', true, net.Token, res => {
      console.log('sessionKey',res);
      if(res.code == 0){

        wx.checkSession({
          success: function () {



            if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
              that.setData({
                modalstatus: true
              });
            } else {//同意授权

              var params = {
                encrypData: encryData,
                ivData: ivData,
                sessionKey: res.data,
              }
              console.log('张张', params);

              net.doRequest(config.service.getWXPhoneUrl2, params, 'POST', true, net.Token, res => {
                console.log('电话',res);

              })
            }
          }
      
        });
        
      }

    });

  }
})